package com.example.musicplayer.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.musicplayer.R;
import com.example.musicplayer.dialogTools.NormalAlertDialog;
import com.example.musicplayer.settings.MusicPlayerSettings;
import com.example.musicplayer.settings.SettingsItem;
import com.example.musicplayer.user.User;
import com.example.musicplayer.view.SettingsView;

public class MusicSettingsActivity extends BaseSettingsActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initSettingsItem();
    }

    @Override
    protected int setActivityTitle() {
        return R.string.title_music_settings_activity;
    }

    @Override
    protected View setSettingsDivideView(String groupLabel) {
        @SuppressLint("InflateParams") View divideView = LayoutInflater.from(this)
                .inflate(R.layout.music_settings_divide_view, null);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , (int) getResources().getDimension(R.dimen.music_settings_divide_height));
        divideView.setLayoutParams(lp);
        ((TextView) divideView.findViewById(R.id.settings_view_group_label)).setText(groupLabel);
        return divideView;
    }

    private void initSettingsItem() {
        //network
        setSettingsDivideItem(getString(R.string.settings_label_network));
        setSettingsItem(SettingsItem.ONLY_WIFI);
        setSettingsItem(SettingsItem.FLOW_REMIND);
        //play and download
        setSettingsDivideItem(getString(R.string.settings_label_play_and_download));
        setSettingsItem(SettingsItem.PLAY_LIST_SETTINGS);
        setSettingsItem(SettingsItem.AUTO_INTO_PLAY_SCREEN);
        setSettingsItem(SettingsItem.ALLOW_PLAY_WITH_OTHER);
        //function and service
        setSettingsDivideItem(getString(R.string.settings_label_function_and_service));
        setSettingsItem(SettingsItem.USE_NOTIFICATION_BAR);
        setSettingsItem(SettingsItem.DESKTOP_LYRIC);
        setSettingsItem(SettingsItem.DESKTOP_LYRIC_LOCK);
        //for manager
        if (User.getInstance().hasAuthority()) {
            setSettingsItem(SettingsItem.DESKTOP_LYRIC_IN_APP);
        }
        //general settings
        setSettingsDivideItem(getString(R.string.settings_label_general_settings));
        setSettingsItem(SettingsItem.FLUENCY_SETTINGS);
        setSettingsItem(SettingsItem.BACKGROUND_SETTINGS);
        setSettingsItem(SettingsItem.PERSONAL_INFO);
        setSettingsItem(SettingsItem.SECURITY_SETTINGS);
    }

    private void showDialogAllowPlayWithOther(final SettingsView settingsView) {
        final NormalAlertDialog alertDialog = new NormalAlertDialog(this, R.string.attention
                , R.string.allow_play_with_other_dialog_text, R.string.cancel, R.string.allow_play_with_other_dialog_confirm);
        alertDialog.getDialog().setCanceledOnTouchOutside(false);
        alertDialog.setDialogTextSize(-2);
        alertDialog.setDialogButtonSize(-2);
        alertDialog.setAdviceButton(false);
        alertDialog.getCancel().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicPlayerSettings.setAllowPlayWithOther(false);
                settingsView.setSwitchChecked(false);
                alertDialog.dismiss();
            }
        });
        alertDialog.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                sendBroadcast(new Intent(getString(R.string.broadcast_allow_play_with_other)));
            }
        });
        alertDialog.show();
    }

    @Override
    protected void updateSettingsItem(SettingsView settingsView) {
        super.updateSettingsItem(settingsView);
        switch (settingsView.getSettingsItem()) {
            case SettingsItem.AUTO_INTO_PLAY_SCREEN:
                settingsView.setSwitchChecked(MusicPlayerSettings.isAutoIntoPlayScreen());
                break;
            case SettingsItem.ALLOW_PLAY_WITH_OTHER:
                settingsView.setSwitchChecked(MusicPlayerSettings.allowPlayWithOther());
                break;
            case SettingsItem.USE_NOTIFICATION_BAR:
                settingsView.setSwitchChecked(MusicPlayerSettings.isUseNotificationBar());
                break;
            case SettingsItem.DESKTOP_LYRIC:
                settingsView.setSwitchChecked(MusicPlayerSettings.isDesktopLyric());
                break;
            case SettingsItem.DESKTOP_LYRIC_LOCK:
                settingsView.setSwitchChecked(MusicPlayerSettings.isDesktopLyricLock());
                settingsView.setVisible(MusicPlayerSettings.isDesktopLyric());
                break;
            case SettingsItem.DESKTOP_LYRIC_IN_APP:
                settingsView.setSwitchChecked(MusicPlayerSettings.isDesktopLyricInApp());
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSettingsItemClick(SettingsView settingsView, boolean isChecked) {
        super.onSettingsItemClick(settingsView, isChecked);
        switch (settingsView.getSettingsItem()) {
            case SettingsItem.ONLY_WIFI:
                break;
            case SettingsItem.FLOW_REMIND:
                break;
            case SettingsItem.AUTO_INTO_PLAY_SCREEN:
                MusicPlayerSettings.setAutoIntoPlayScreen(isChecked);
                break;
            case SettingsItem.ALLOW_PLAY_WITH_OTHER:
                MusicPlayerSettings.setAllowPlayWithOther(isChecked);
                if (isChecked) {
                    showDialogAllowPlayWithOther(settingsView);
                } else {
                    sendBroadcast(new Intent(getString(R.string.broadcast_allow_play_with_other)));
                }
                break;
            case SettingsItem.USE_NOTIFICATION_BAR:
                MusicPlayerSettings.setUseNotificationBar(isChecked);
                sendBroadcast(new Intent(getString(R.string.broadcast_use_notification_bar)));
                break;
            case SettingsItem.DESKTOP_LYRIC:
                MusicPlayerSettings.setDesktopLyric(isChecked);
                SettingsView lyricLockView = getSettingsView(SettingsItem.DESKTOP_LYRIC_LOCK);
                if (lyricLockView != null) {
                    lyricLockView.showOrHide(isChecked);
                }
                sendBroadcast(new Intent(getString(R.string.broadcast_desktop_lyric)));
                break;
            case SettingsItem.DESKTOP_LYRIC_LOCK:
                MusicPlayerSettings.setDesktopLyricLock(isChecked);
                sendBroadcast(new Intent(getString(R.string.broadcast_desktop_lyric_lock)));
                break;
            case SettingsItem.DESKTOP_LYRIC_IN_APP:
                MusicPlayerSettings.setDesktopLyricInApp(isChecked);
                sendBroadcast(new Intent(getString(R.string.broadcast_desktop_lyric_in_app)));
                break;
            case SettingsItem.FLUENCY_SETTINGS:
                startActivity(new Intent(this, FluencySettingsActivity.class));
                break;
            case SettingsItem.BACKGROUND_SETTINGS:
                startActivity(new Intent(this, BackgroundSettingsActivity.class));
                break;
            case SettingsItem.PERSONAL_INFO:
                startActivity(new Intent(this, PersonalInfoActivity.class));
                break;
            case SettingsItem.SECURITY_SETTINGS:
                startActivity(new Intent(this, SecuritySettingsActivity.class));
                break;
            case SettingsItem.PLAY_LIST_SETTINGS:
                startActivity(new Intent(this, PlayListSettingsActivity.class));
                break;
            default:
                break;
        }
    }
}